/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  							 script_change_owner.cpp

	$Header: /heroes4/script_change_owner.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "script_change_owner.h"

#include "army.h"
#include "adventure_map.h"
#include "sized_int_types.h"
#include "streambuf_operators.h"
#include "town.h"

// --------------------------------------------------------------------------
// t_script_change_owner members
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool t_script_change_owner::read_from_map(
	std::streambuf &	stream,
	int					format_version )
{
	m_owner_color = t_player_color( get< t_uint8 >( stream ) );
	return m_owner_color >= 0 && m_owner_color < k_player_color_count;
}

bool t_script_change_owner::read( std::streambuf& stream, int version )
{
	m_owner_color = get< t_player_color >( stream );
	return m_owner_color >= 0 && m_owner_color < k_player_color_count;
}

bool t_script_change_owner::write( std::streambuf& stream ) const
{
	if (!t_abstract_script_action::write( stream ))
		return false;

	put< t_player_color >( stream, m_owner_color );
	
	return true;
}

void t_script_change_owner::execute(t_script_context_hero const& context) const
{
	assert(false);
}

void t_script_change_owner::execute(t_script_context_town const& context) const
{
	if (context.object) {
		assert(context.map);
		context.object->set_owner(context.map->get_player_number(get_owner_color()));
	}
}

void t_script_change_owner::execute(t_script_context_object const& context) const
{
	if (context.object) {
		assert(context.map);
		context.object->set_owner(context.map->get_player_number(get_owner_color()));
	}
}

void t_script_change_owner::execute(t_script_context_army const& context) const
{
	assert( context.map && context.army );

	if (context.army->get_owner() == NULL)
	{
		assert(context.map);
		context.army->get_army()->set_owner(context.map->get_player_number(get_owner_color()));
	}
}

void t_script_change_owner::execute(t_script_context_global const& context) const
{
	assert(false);
}


